package top.wineye.spring.dependency.inject;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
 * 通过 {@link org.springframework.beans.factory.Aware} Aware 接口注入对应的依赖
 */
public class AwareInterfaceInject implements BeanFactoryAware, ApplicationContextAware {

    BeanFactory bf;
    ApplicationContext appCtx;

    public static void main(String[] args) {
        AnnotationConfigApplicationContext ctx =
                new AnnotationConfigApplicationContext(AwareInterfaceInject.class);


        AwareInterfaceInject thisBean = ctx.getBean(AwareInterfaceInject.class);
        System.out.println(thisBean.bf);
        System.out.println(thisBean.appCtx);

        System.out.println(thisBean.bf == ctx.getBeanFactory());
        System.out.println(thisBean.appCtx == ctx);

        ctx.close();
    }

    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        this.bf = beanFactory;
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.appCtx = applicationContext;
    }
}
